package com.java.module.user.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.java.common.service.ApiTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.java.common.lang.domain.entity.User;
import com.java.common.utils.AssertKit;
import com.java.common.utils.DateUtils;
import com.java.module.user.domain.Message;
import com.java.module.user.domain.MessageAssign;
import com.java.module.user.mapper.MessageMapper;
import com.java.module.user.service.IMessageAssignService;
import com.java.module.user.service.IMessageService;
import com.java.module.user.service.IUserService;

/**
 * 基础：消息Service业务层处理
 * 
 * @author Administrator
 * @date 2021-03-24
 */
@Service
public class MessageServiceImpl implements IMessageService {

    private final Integer ALL_PUSH_TYPE = 0;

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private IUserService userService;

    @Autowired
    private IMessageAssignService messageAssignService;


    @Autowired
    ApiTokenService tokenService;

    /**
     * 查询基础：消息
     * 
     * @param mid 基础：消息ID
     * @return 基础：消息
     */
    @Override
    public Message selectMessageById(Long mid) {
        return messageMapper.selectMessageById(mid);
    }

    /**
     * 查询基础：消息列表
     * 
     * @param message 基础：消息
     * @return 基础：消息
     */
    @Override
    public List<Message> selectMessageList(Message message) {
        return messageMapper.selectMessageList(message);
    }

    /**
     * 查询单个基础：消息
     * 
     * @param message 基础：消息
     * @return 基础：消息
     */
    @Override
    public Message selectMessage(Message message) {
        return messageMapper.selectMessage(message);
    }

    /**
     * 新增基础：消息
     * 
     * @param message 基础：消息
     * @return 结果 @ zt
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertMessage(Message message) {
        Date nowDate = DateUtils.getNowDate();

        message.setCreateTime(nowDate);
        messageMapper.insertMessage(message);

        List<MessageAssign> messageAssignList = new ArrayList<MessageAssign>(16);

        // 0全部推送 1选择推送
        Integer pushType = message.getPushType();
        if (ALL_PUSH_TYPE.equals(pushType)) {
            List<User> userList = userService.selectUserList(null);
            for (User user : userList) {
                MessageAssign messageAssign = new MessageAssign();
                messageAssign.setCreateTime(nowDate);
                messageAssign.setMid(message.getMid());
                messageAssign.setUid(user.getUid());
                messageAssignList.add(messageAssign);
            }
        } else {
            Long[] uids = message.getUids();
            for (Long uid : uids) {
                MessageAssign messageAssign = new MessageAssign();
                messageAssign.setCreateTime(nowDate);
                messageAssign.setMid(message.getMid());
                messageAssign.setUid(uid);
                messageAssignList.add(messageAssign);
            }
        }

        AssertKit.notEmpty(messageAssignList, "添加失败：没有用户。");

        return messageAssignService.insertMessageAssignByUids(messageAssignList);
    }

    /**
     * 修改基础：消息
     * 
     * @param message 基础：消息
     * @return 结果
     */
    @Override
    public int updateMessage(Message message) {
        message.setUpdateTime(DateUtils.getNowDate());
        return messageMapper.updateMessage(message);
    }

    /**
     * 批量删除基础：消息
     * 
     * @param mids 需要删除的基础：消息ID
     * @return 结果
     */
    @Override
    public int deleteMessageByIds(Long[] mids) {
        return messageMapper.deleteMessageByIds(mids);
    }

    /**
     * 删除基础：消息信息
     * 
     * @param mid 基础：消息ID
     * @return 结果
     */
    @Override
    public int deleteMessageById(Long mid) {
        return messageMapper.deleteMessageById(mid);
    }

    @Override
    public int insertMessageFriend(Message message) {
        Date nowDate = DateUtils.getNowDate();
        message.setCreateTime(nowDate);
        messageMapper.insertMessage(message);
        List<MessageAssign> messageAssignList = new ArrayList<MessageAssign>(16);
        //精准推送
        MessageAssign messageAssign = new MessageAssign();
        messageAssign.setCreateTime(nowDate);
        messageAssign.setMid(message.getMid());
        messageAssign.setUid(message.getToUid());
        messageAssignList.add(messageAssign);
        AssertKit.notEmpty(messageAssignList, "添加失败：没有用户。");

        return messageAssignService.insertMessageAssignByUids(messageAssignList);
    }

}